﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using COMOSOFT.Class;


namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class PopUpMA : Form
    {
        ClassShare cls = new ClassShare("PopUpMA");
        int MALID,MAID;       
        string Sizes = null;
        struct SizeNode
        {
            public int OID;
            public int[] SBIDs;
        }
        List<SizeNode> ListSize = new List<SizeNode>();
        public PopUpMA(int _MALID,int _MAID)
        {
            InitializeComponent();
            this.MALID = _MALID;
            this.MAID = _MAID;       
        }

        #region [Insert MALDetails]
        private void bntOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridViewPO.RowCount > 0)
                {
                    for (int i = 0; i < gridViewPO.RowCount; i++)
                    {
                       
                        int OID = Convert.ToInt32(gridViewPO.GetDataRow(i)["OID"]);
                        int MADID = Convert.ToInt32(gridViewPO.GetDataRow(i)["Color"]);
                        double EstCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["EstCons"]);
                        double CusCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["CusCons"]);
                        double ActCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["ActCons"]);
                        double Wastage = Convert.ToDouble(gridViewPO.GetDataRow(i)["Wastage"]);
                        
                         DataTable dtb=cls.GetData("sp_MAListDetailInsert", new string[8] { "@MALID", "@OID", "@MAID", "@MADID", "@EstCons", "@CusCons", "@ActCons", "@Wastage" }, new object[8] { MALID, OID, MAID, MADID, EstCons, CusCons, ActCons, Wastage });
                         foreach (SizeNode node in ListSize)
                         {
                             if (node.OID == OID)
                             {
                                 foreach (int sbid in node.SBIDs)
                                 {
                                     cls.UpdateData("sp_MASizeUsDetailsUpdate", new string[2] { "@SBID", "@MALDID" }, new object[2] {sbid, Convert.ToInt32(dtb.Rows[0][0]) });               
                                 }
                             }
                         }                       
                    }
                }
                MessageBox.Show("Insert Successfully!!");               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n You must choose color!", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Close();
            }
        }
        #endregion

        #region [Load PopUp]
        private void PopUpMA_Load(object sender, EventArgs e)
        {
            sp_ChooseMASourceTableAdapter.Fill(dataSetMatarial.sp_ChooseMASource, MALID);
            sp_ChooseColorTableAdapter.Fill(dataSetMatarial.sp_ChooseColor, MAID);           
        }
        #endregion//

        #region [Show SizeBreakdown]
        private void gridViewPO_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridViewPO.FocusedRowHandle >= 0)
            {
                int OID = Convert.ToInt32(gridViewPO.GetFocusedDataRow()["OID"]);
                orderSizeBreakdownTableAdapter.Fill(dataSetMatarial.OrderSizeBreakdown, OID);
            }            
        }
        #endregion

        #region [Choose Size] 
        private void bntSizeOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView_Size.FocusedRowHandle > 0)
                {
                    int[] selectrow = gridView_Size.GetSelectedRows();
                    SizeNode node = new SizeNode();
                    node.OID = Convert.ToInt32(gridViewPO.GetFocusedDataRow()["OID"]);
                    node.SBIDs = new int[selectrow.Count()];
                    int set = 0;                   
                    foreach (int row in selectrow)
                    {
                        node.SBIDs[set] = Convert.ToInt32(gridView_Size.GetDataRow(row)["SBID"]);
                        if (set == 0)
                        {                           
                            Sizes = gridView_Size.GetDataRow(row)["Inseam"].ToString() + "-" + gridView_Size.GetDataRow(row)["Size"].ToString();
                        }
                        else
                        {                          
                            Sizes += "," + gridView_Size.GetDataRow(row)["Inseam"].ToString() + "-" + gridView_Size.GetDataRow(row)["Size"].ToString();
                        }
                        set++;
                    }
                    gridViewPO.SetFocusedRowCellValue("Size", Sizes);
                    ListSize.Add(node);
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        private void bntExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
